package com.wx.activitys.base.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * DESCRIPTION:
 * AUTHOR: Qinlong
 * DATE: 2017/8/25
 */
public class BaseUtils {
    /**
     * Md5
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String Md5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(str.getBytes());
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : bytes) {
            int bt = b & 0xff;
            if (bt < 16) {
                stringBuffer.append(0);
            }
            stringBuffer.append(Integer.toHexString(bt));
        }
        return stringBuffer.toString();
    }

    /**
     * 发送短信签名
     *使用 企业ID+KEY+时间戳 生成MD5字符串作为签名。MD5生成32位，且需要小写。
     * @param account
     * @param key
     * @param timeStamp
     * @return
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static String getSign(String account,String key,String timeStamp) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return Md5(account + key + timeStamp);
    }

    /**
     * 随机生成六位数
     * @return
     */
    public static int getRandNum() {
        return (int)((Math.random()*9+1)*100000);
    }
}
